package com.fr.lucene;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;

public class IndexWriterTest {
	
	private static final String filePath = "F:\\sign data\\biz_district_sign.csv";
	
	private static final String indexPath = "F:\\lucene index\\sign";
	
	public static void main(String[] args) throws IOException {
		Analyzer ana = new StandardAnalyzer();
		IndexWriterConfig config = new IndexWriterConfig(ana);
		Path path = Paths.get(indexPath);
		FSDirectory d = FSDirectory.open(path);
		IndexWriter writer = new IndexWriter(d, config);
//		index(writer);
//		d.close();
		writer.commit();
		writer.close();
	}

	private static void index(IndexWriter writer) throws IOException {
		FileReader fr = new FileReader(filePath);
		BufferedReader br = new BufferedReader(fr);
		String line = br.readLine();
		List<Document> docs = new ArrayList<>();
		while(line != null) {
			if(line.startsWith("#")) {
				line = br.readLine();
				continue;
			}
			String[] rs = line.split(",");
			if(rs.length > 10) {
				Document doc = new Document();
				doc.add(new NumericDocValuesField("signId", Long.parseLong(rs[0])));
				doc.add(new NumericDocValuesField("districtId", Long.parseLong(rs[1])));
				doc.add(new NumericDocValuesField("signType", Long.parseLong(rs[2])));
				doc.add(new TextField("signName", rs[3], Field.Store.YES));
				doc.add(new StringField("signId", rs[9], Field.Store.YES));
				docs.add(doc);
				if(docs.size() > 1000) {
					writer.addDocuments(docs);
					docs.clear();
				}
			}
			line = br.readLine();
		}
		if(docs.size() > 0) {
			writer.addDocuments(docs);
		}
		br.close();
		fr.close();
	}

}
